var app=angular.module('myApp', []).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider.
      when('/', {templateUrl: 'opbal_tran_ajs.html', controller: ListOpbalMstCtrl}).
      when('/deleteopbal/', {templateUrl: 'opbal_tran_ajs.html', controller: ListOpbalMstCtrl}).
      when('/editopbal/', {templateUrl: 'opbal_tran_ajs.html', controller: ListOpbalMstCtrl}). 
      when('/updtopbal/', {templateUrl: 'opbal_tran_ajs.html', controller: ListOpbalMstCtrl}). 
      when('/cancel/', {templateUrl: 'opbal_tran_ajs.html', controller: ListOpbalMstCtrl}). 
      otherwise({redirectTo: '/'});
}]);


app.service('ContactService', function () 
	{
     this.get = function (opbaltranid,$window) {
        for (i in opbalmast) {
            if (opbalmast[i].opbaltranid == opbaltranid) {
                 $window.alert("Edit Opening Balance Details  "+opbaltranid);
                return opbalmast[i];
            }
        }
     };
	}
);


function ListOpbalMstCtrl($scope, $http,$window,ContactService,$location)
 {
    $scope.currentPage = 0;
    $scope.pageSize = 25;
    $scope.newopbalmast = {};
	$scope.comment = "";
	$scope.trancat = [{ id: 1, name: 'BALANCE UPLOAD'},{ id: 2, name: 'CREDIT'},{ id: 3, name: 'DEBIT'},{ id: 4, name: 'TICKET BOOKING'}, { id: 5, name: 'TOP UP'} ,{ id: 6, name: 'TRANSFER'}, { id: 7, name: 'OTHERS'} ];
	
	$scope.Math = window.Math;
    $http.get('opbal_tran_ajs_selectdata.php').success(function(data) {
    $scope.opbalmast = data;
    });
	
    $http.get('customeriddata.php').success(function(data1) {
    $scope.customeridmast = data1;
    });  
	
	/*  $http.get('customerdata.php').success(function(data1) {
    $scope.customermast = data1;
    }); */
	
       
     //Function to Edit Record in the Opening Balance Master Screen 
    $scope.edit = function(opbaltranid,comment) {
         for(i in $scope.opbalmast) {
          if($scope.opbalmast[i].opbaltranid == opbaltranid)
            {
            //  $window.alert(" value of opbaltranid is "+i+ "  "+opbaltranid+" " +comment); 
			  //we use angular.copy() method to create copy of original object
               $scope.newopbalmast = angular.copy($scope.opbalmast[i]);
				$scope.comment = comment;
            }
        }
            
    };
    
	
    //Function to Delete Record in the Opening Balance Master based on the opbaltranid 
   $scope.deleteopbal = function(item) {
      if (confirm('Do you really want to delete this Opening Balance Master record?')) 
        {
            $window.alert(item+' Record is being Deleted');
            $http.post('opbal_tran_ajs_deletedata.php',{'opbaltranid': item}).success(function(data)
            {
                $window.alert( 'Record for ' + item+ 'got deleted Successfully');
                 $scope.activePath = $location.path('opbal_tran_ajs.php#/editopbal/');
                 window.location.href = 'opbal_tran_ajs.php#/editopbal/';
            });
               
        }
      };
      
	
	//Function to Update Data in the Opening Balance Master  
		$scope.updtopbaldata = function(newopbalmast,comment) 
			{   
				$window.alert("Updation of Record for "+newopbalmast.opbaltranid+",  "+newopbalmast.opbaldate +",  "+newopbalmast.trancat + ",  "+ newopbalmast.amtpaid +",  " +newopbalmast.amtreceived  + " , "+comment+ ",  "+newopbalmast.custid );
				var sql = 'opbal_tran_ajs_updatedata.php?'+'custid='+newopbalmast.custid+'&opbaldate='+newopbalmast.opbaldate +'&amtpaid='+newopbalmast.amtpaid + '&amtreceived='+newopbalmast.amtreceived+'&trancat='+newopbalmast.trancat+'&comment='+comment+'&opbaltranid='+newopbalmast.opbaltranid;
				//$window.alert(sql);
				$http.post(sql).success(function(data)  
				{
					$window.alert( 'Record for Opening Balance Tran id  '+ newopbalmast.opbaltranid  + '  got Successfully Updated');
					window.location.href = 'opbal_tran_ajs.php#/editopbal/';
				});
			};
      
	 //Function to Insert Data in the Opening Balance Master  
	   $scope.saveopbaldata = function(newopbalmast,comment) 
	  	{   
            $window.alert("Save Record for "+newopbalmast.opbaldate +","+newopbalmast.trancat + ","+ newopbalmast.custid + ","+ newopbalmast.amtpaid +"," +newopbalmast.amtreceived + comment);
			var sql = 'opbal_tran_ajs_insertdata.php?'+'&opbaldate='+newopbalmast.opbaldate+'&trancat='+newopbalmast.trancat +'&amtpaid='+newopbalmast.amtpaid +'&amtreceived='+newopbalmast.amtreceived+'&comment='+comment+'&custid='+newopbalmast.custid;
			//$window.alert(sql);
			$http.post(sql).success(function(data)  
			{
				$window.alert( 'Record for '+  newopbalmast.custid + "  ,  " + newopbalmast.trancat+ 'got Successfully Saved');
				window.location.href = 'opbal_tran_ajs.php#/editopbal/';
			});
		};
	  
	  //Function to Clear the selected parameters in the search screen.
        $scope.cancel= function()
		{
			$scope.activePath = null;
			$scope.activePath = $location.path('opbal_search_ajs.php#/editopbal/');
			window.location.href = '#/editopbal/';
		};
      
 }
    
    //Function to convert the entered value in UI into uppercase
     app.directive('capitalize', function () {
        return {
            require: 'ngModel',
            link: function (scope, element, attrs, modelCtrl) {
                var capitalize = function (inputValue) {
                    if (angular.isUndefined(inputValue))
                        return;
 
                    var capitalized = inputValue.toUpperCase();
                    if (capitalized !== inputValue) {
                        modelCtrl.$setViewValue(capitalized);
                        modelCtrl.$render();
                    }
                    return capitalized;
                };
                modelCtrl.$parsers.push(capitalize);
                capitalize(scope[attrs.ngModel]);  // capitalize initial value
            }
        };
    });
    
     //Custom Filter Function to sort the record.
    app.filter('startingFrom', function() 
    {
            return function(input, start) 
            {
                start = +start; //parse to int
                return input.slice(start);
            };
    });
        